﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using thinger.HeatingControlProject._4.实体类;

namespace thinger.HeatingControlProject._2.数据层
{
    /// <summary>
    /// 用户信息数据类
    /// </summary>
    public class SysAdminService
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public SysAdmin AdminLogin(SysAdmin admin)
        {
            //封装SQL
            string sql = "select LoginName,RoleName from SysAdmin " +
                "where LoginId = @LoginId and LoginPwd = @LoginPwd";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@LoginId",admin.LoginId),
                new SqlParameter("@LoginPwd",admin.LoginPwd)
            };
            //提交查询
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, param);
            //封装对象
            if (reader.Read())
            {
                admin.LoginName = reader["LoginName"].ToString();
                admin.RoleName = reader["RoleName"].ToString();
            }
            else
            {
                admin = null;
            }
            reader.Close();
            return admin;
        }

        //显示所有的用户对象数据
        public List<SysAdmin> QuerySysAdmins() 
        {
            string sql = "select LoginId,LoginName,LoginPwd,RoleName from SysAdmin";
            SqlDataReader reader = SqlHelper.ExecuteReader(sql);
            List<SysAdmin> sysAdmins = new List<SysAdmin>();
            while (reader.Read())
            {
                sysAdmins.Add(new SysAdmin()
                {
                    LoginId = Convert.ToInt32(reader["LoginId"]),
                    LoginName = reader["LoginName"].ToString(),
                    LoginPwd = reader["LoginPwd"].ToString(),
                    RoleName = reader["RoleName"].ToString()
                });
            }
            reader.Close();
            return sysAdmins;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int AddSysAdmin(SysAdmin sysAdmin)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("insert into SysAdmin(LoginName,LoginPwd,RoleName)");
            stringBuilder.Append(" values(@LoginName,@LoginPwd,@RoleName)");
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@LoginName",sysAdmin.LoginName),
                new SqlParameter("@LoginPwd",sysAdmin.LoginPwd),
                new SqlParameter("@RoleName",sysAdmin.RoleName)
            };
            return SqlHelper.ExecuteNonQuery(stringBuilder.ToString(), param);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int ModifySysAdmin(SysAdmin sysAdmin)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("update SysAdmin set LoginName = @LoginName,LoginPwd = @LoginPwd,RoleName = @RoleName");
            stringBuilder.Append(" where LoginId = @LoginId");
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@LoginName",sysAdmin.LoginName),
                new SqlParameter("@LoginPwd",sysAdmin.LoginPwd),
                new SqlParameter("@RoleName",sysAdmin.RoleName),
                new SqlParameter("@LoginId",sysAdmin.LoginId)
            };
            return SqlHelper.ExecuteNonQuery(stringBuilder.ToString(), param);
        }
        //删除用户
        public int DeleteSysAdmin(int LoginId)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("delete from SysAdmin where LoginId = @LoginId");
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@LoginId",LoginId)
            };
            return SqlHelper.ExecuteNonQuery(stringBuilder.ToString(), param);
        }
    }
}
